﻿// -----------------------------------------------------------------------
// <copyright file="ResumeHandler.cs" company="Zen Design Corp">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Zen.BlackBerry.Subscription.Web
{
	using System;
	using System.Threading.Tasks;
	using System.Web;
	using Microsoft.Practices.ServiceLocation;

	/// <summary>
	/// Responsible for handling a resume subscription operation.
	/// </summary>
	/// <remarks>
	/// <para>
	/// The following HTTP request parameters are needed for a resume: appid. 
	/// If not using container-managed authentication, the following two HTTP request parameters will also be needed: username, and password. 
	/// </para>
	/// <para>
	/// The following return codes can come back from this servlet: SUCCESS, INVALID_PUSH_APPLICATION, INVALID_SUBSCRIBER_ID, SUBSCRIBER_NOT_FOUND, CANNOT_RESUME_SINCE_UNSUBSCRIBED, INVALID_USERNAME, INVALID_PASSWORD, SUBSCRIPTION_NOT_SUPPORTED, SUBSCRIBER_AUTHENTICATION_FAILED, CONTENT_PROVIDER_SUBSCRIPTION_FAILED, SYSTEM_ERROR
	/// </para>
	/// </remarks>
	public class ResumeHandler : SubscriptionHandler
	{
		/// <summary>
		/// Gets the type of the subscription.
		/// </summary>
		/// <value>The type of the subscription.</value>
		public override SubscriptionType SubscriptionType
		{
			get
			{
				return SubscriptionType.Resume;
			}
		}

		/// <summary>
		/// Gets the subscription operation details parsed out of the HTTP
		/// request.
		/// </summary>
		/// <param name="context">The context.</param>
		/// <returns>
		/// A <see cref="BaseRequest"/> containing the subscription details.
		/// </returns>
		protected override BaseRequest GetRequest(HttpContext context)
		{
			return SubscriptionRequestParser.ParseResumeRequest(context.Request);
		}

		/// <summary>
		/// Asynchronously performs the subscription operation.
		/// </summary>
		/// <param name="context">The context.</param>
		/// <param name="request">The request.</param>
		/// <returns></returns>
		protected override async Task PerformSubscriptionOperation(
			HttpContext context, BaseRequest request)
		{
			ResumeRequest resumeRequest = (ResumeRequest)request;
			try
			{
				await SubscriptionService.ResumeSubscription(resumeRequest);
				SetResponse(context.Response, SubscriptionStatusCode.Success);
			}
			catch (Exception)
			{
				SetResponse(context.Response, SubscriptionStatusCode.SystemError);
			}
		}
	}
}
